package com.hui.security.sys.service.impl;

import com.hui.security.sys.entity.Group;
import com.hui.security.sys.exception.ExceptionEnum;
import com.hui.security.sys.exception.GroupException;
import com.hui.security.sys.mapper.GroupMapper;
import com.hui.security.sys.service.GroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * <b><code>GroupServiceImpl</code></b>
 * <p/>
 * Description:
 * <p/>
 * <b>Creation Time:</b> 2019/1/5 0:27.
 *
 * @author HuWeihui
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Autowired
    private GroupMapper groupMapper;

    /**
     * 新增用户组
     *
     * @param group
     * @return
     */
    @Override
    public Group add(Group group) {
        if (null != groupMapper.selectByGroupName(group.getGroupName())) {
            throw new GroupException(ExceptionEnum.GROUP_ERROR_REPEAT_GROUP);
        }
        groupMapper.insertSelective(group);
        return group;
    }

    /**
     * 删除用户组
     *
     * @param groupId
     */
    @Override
    public void delete(String groupId) {
        groupMapper.deleteByPrimaryKey(groupId);
    }

    /**
     * 更新用户组
     *
     * @param group
     */
    @Override
    public void update(Group group) {
        if (null != groupMapper.selectByGroupName(group.getGroupName())) {
            throw new GroupException(ExceptionEnum.GROUP_ERROR_REPEAT_GROUP);
        }
        groupMapper.updateByPrimaryKey(group);
    }

    /**
     * 查询用户组
     *
     * @param groupId
     * @return
     */
    @Override
    public Group get(String groupId) {
        return groupMapper.selectByPrimaryKey(groupId);
    }

    /**
     * 查询用户相关用户组
     *
     * @param userId
     * @return
     */
    @Override
    public List<Group> listByUserId(String userId) {
        return groupMapper.selectByUserId(userId);
    }
}
